package com.jixiantubu.adapter;

import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.jixiantubu.bean.ReplyInfoBean;
import com.jixiantubu.luntan.Reply_replyActivity;
import com.jixiantubu.util.MyApplication;
import com.jixiantubu.util.NoScrollListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Reply_TieziAdapter extends BaseAdapter {

	List<ReplyInfoBean> pList, all_list = new ArrayList<ReplyInfoBean>();
	Context context;
	LayoutInflater mInflater;
	ViewHolder mHolder;
	Reply_ReplyAdapter adapter;
	HttpUtils httpUtils;
	private int resourceId;
	int topic;
	int rpid;
	int uid;
	String username;
	BitmapUtils bitmapUtils;
	ReplyInfoBean replyInfoBean;
	RequestParams params;
	List<ReplyInfoBean> list2;

	public Reply_TieziAdapter(List<ReplyInfoBean> all_list,
			List<ReplyInfoBean> pList, int resourceId, Context context) {
		super();
		this.all_list = all_list;
		this.context = context;
		this.resourceId = resourceId;
		this.pList = pList;
		this.mInflater = LayoutInflater.from(context);
	}

	public List<ReplyInfoBean> getList() {
		return pList;
	}

	public void setList(List<ReplyInfoBean> list) {
		this.pList = list;
	}

	@Override
	public int getCount() {
		return pList.size();
	}

	@Override
	public Object getItem(int position) {
		return pList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(resourceId, null);

			mHolder = new ViewHolder();
			mHolder.imageView1 = (ImageView) convertView
					.findViewById(R.id.reply_user_imgview);
			mHolder.imageView2 = (ImageView) convertView
					.findViewById(R.id.reply_imgview);
			mHolder.imageView3 = (ImageView) convertView
					.findViewById(R.id.reply_info_img);
			mHolder.textView1 = (TextView) convertView
					.findViewById(R.id.reply_username_textview);
			mHolder.textView2 = (TextView) convertView
					.findViewById(R.id.reply_rank_textview);
			mHolder.textView3 = (TextView) convertView
					.findViewById(R.id.reply_createTime_textview);
			mHolder.textView4 = (TextView) convertView
					.findViewById(R.id.reply_content_textview);
			mHolder.listView = (NoScrollListView) convertView
					.findViewById(R.id.reply_huifutiezi_listview);
			convertView.setTag(mHolder);
		} else {
			mHolder = (ViewHolder) convertView.getTag();
		}
		bitmapUtils = new BitmapUtils(context);
		mHolder.textView1.setText(pList.get(position).getReplyname());
		mHolder.textView2.setText("第" + (pList.get(position).getRank() + 1)
				+ "楼");
		mHolder.textView3.setText(pList.get(position).getCreateTime());
		mHolder.textView4.setText(pList.get(position).getContent());
		String url = "http://" + MyApplication.getIP() + ":8080/Jinnang/img/"
				+ pList.get(position).getReplyname_imgpath();
		bitmapUtils.display(mHolder.imageView1, url);
		if (pList.get(position).getContent_imgpath() == null) {
			mHolder.imageView3.setVisibility(View.GONE);
		} else {
			String url1 = "http://" + MyApplication.getIP()
					+ ":8080/Jinnang/img/"
					+ pList.get(position).getContent_imgpath();
			bitmapUtils.display(mHolder.imageView3, url1);
		}
		mHolder.imageView2.setId(R.drawable.huifu);
		mHolder.imageView2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(context, Reply_replyActivity.class);
				intent.putExtra("reply_post", pList.get(position));
				context.startActivity(intent);
			}
		});
		list2 = new ArrayList<ReplyInfoBean>(0);
		replyInfoBean = pList.get(position);
		for (ReplyInfoBean replyInfo : all_list) {
			if (replyInfo.getRp_id() == replyInfoBean.getRp_id()
					&& replyInfo.getRank() == 0) {
				list2.add(replyInfo);
			}
		}
		Log.e("-------<<<<<<<<", list2.toString());
		adapter = new Reply_ReplyAdapter(context, list2,
				replyInfoBean.getName());
		mHolder.listView.setAdapter(adapter);
		setListViewHeightBasedOnChildren(mHolder.listView);
		return convertView;
	}

	class ViewHolder {
		ImageView imageView1, imageView2, imageView3;
		TextView textView1, textView2, textView3, textView4;
		NoScrollListView listView;
	}
	private void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
		Log.e(">>>>>>>>>>>>>>>", String.valueOf(params.height));
	}
}
